﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2010
// admin@franknagl.de
//
namespace SBIP.Filter
{
    using System;

    /// <summary>
    /// Discrete laplace operator.
    /// </summary>
    /// <remarks>
    /// <para>Discrete laplace operator is often used in image processing e.g. in 
    /// edge detection and motion estimation applications. The discrete laplacian is 
    /// defined as the sum of the second derivatives and calculated as sum of 
    /// differences over the nearest neighbours of the central pixel.
    /// </para>
    /// <para>Therefore two versions of kernels exists:</para>
    /// <code lang="none">
    ///    V1         V2
    /// 0  1  0     1  1  1
    /// 1 -4  1     1 -8  1
    /// 0  1  0     1  1  1
    /// </code>
    /// 
    /// <para>Sample usage:</para>
    /// <code>
    /// Bitmap image = new Bitmap("cathedral.jpg");
    ///
    /// // 1. Offline rendering of images
    /// // create Processor, used as rendering framework
    /// Processor processor = new Processor( );
    /// // starts Processor
    /// processor.Begin( image );
    /// // create Laplace filter
    /// Laplace filter = new Laplace( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    /// filter.Version = Laplace.Versions.Normal;
    /// filter.Factor = 3.0f;
    /// // apply the filter, render to bitmap  
    /// Bitmap resultImage = processor.RenderToBitmap( );
    /// resultImage.Save( "Laplace.jpg", ImageFormat.Jpeg );
    /// // apply the filter, render to texture
    /// // Texture2D resultTexture = processor.RenderToTexture( );
    /// processor.End( );
    ///  
    /// // 2. Online rendering of videos or images
    /// // create any windows control for rendering in
    /// Form myForm = new Form
    /// {
    ///     Width = 1280,
    ///     Height = 720
    /// };
    /// // starts Processor
    /// processor.Begin( "Video.avi", myForm );
    /// // create Laplace filter
    /// filter = new Laplace( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    /// filter.Version = Laplace.Versions.WithDiagonals;
    /// filter.Factor = 2.0f;
    /// // apply the filter
    /// myForm.Show( );
    /// while ( myForm.Created )
    /// {
    ///     processor.Render( );
    ///     Application.DoEvents( );
    /// }
    /// processor.End( );
    /// </code>
    /// 
    /// <para><b>Initial image:</b></para>
    /// <img src="../../Cathedral.jpg" width="500" height="338" />
    /// <para><b>Result image:</b></para>
    /// <img src="../../Laplace.jpg" width="500" height="338" />
    /// </remarks>
    public sealed class Laplace : BaseFilter
    {
        /// <summary>Two different versions of Laplace filter.</summary>
        public enum Versions
        {
            /// <summary>Normal kernel without diagonal neighbours.</summary>
            Normal,
            /// <summary>Kernel including diagonal neighbours.</summary>
            WithDiagonals
        }

        #region effect properties
        private Versions version;
        /// <summary>The version of Laplace filter.</summary>
        public Versions Version
        {
            get { return version; }
            set
            {
                if (Effect == null)
                    throw new ArgumentException("Version" + InitMsg);

                version = value;
                Effect.Parameters["Version"].SetValue((int)Version);
            }
        }

        private float factor;
        /// <summary>
        /// Multiplication factor for stronger lines in edge image.
        /// </summary>
        /// <remarks>Default value: 1.0</remarks>
        public float Factor
        {
            get { return factor; }
            set
            {
                if (Effect == null)
                    throw new ArgumentException("Factor" + InitMsg);

                factor = value;
                Effect.Parameters["Factor"].SetValue(value);
            }
        }
        #endregion effect properties

        /// <summary>
        /// Initializes a new instance of the <see cref="Laplace"/> class.
        /// </summary>
        public Laplace() : base("Laplace") { }

        /// <summary>
        /// Defines Laplace algorithm properties.
        /// </summary>
        protected override void PostInit()
        {
            Factor = 1.0f;
            Version = Versions.Normal;
        }
    }
}
